package com.example.personalcenter;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class HomeActivity extends AppCompatActivity {
    private static final String TAG = "HomeActivity";
    private static final String KEY_CURRENT_FRAGMENT = "current_fragment";
    private static final int FRAGMENT_HOME = 0;
    private static final int FRAGMENT_PROFILE = 1;
    
    private String username;
    private Button btnNavHome;
    private Button btnNavProfile;
    private SharedPreferences sharedPreferences;
    
    private HomeFragment homeFragment;
    private ProfileFragment profileFragment;
    private int currentFragment = FRAGMENT_HOME; // 当前显示的Fragment

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.activity_home);

        // 接收用户名并保存到SharedPreferences
        username = getIntent().getStringExtra("username");
        sharedPreferences = getSharedPreferences("PersonalCenter", Context.MODE_PRIVATE);
        
        // 如果有用户名，保存到SharedPreferences
        if (username != null && !username.isEmpty()) {
            sharedPreferences.edit().putString("username", username).apply();
        }

        // 初始化底部导航按钮
        btnNavHome = findViewById(R.id.btn_nav_home);
        btnNavProfile = findViewById(R.id.btn_nav_profile);

        // 处理屏幕旋转，避免Fragment重叠
        if (savedInstanceState != null) {
            // 恢复保存的Fragment
            homeFragment = (HomeFragment) getSupportFragmentManager().findFragmentByTag("HOME_FRAGMENT");
            profileFragment = (ProfileFragment) getSupportFragmentManager().findFragmentByTag("PROFILE_FRAGMENT");
            currentFragment = savedInstanceState.getInt(KEY_CURRENT_FRAGMENT, FRAGMENT_HOME);
        } else {
            // 首次创建，显示首页Fragment
            homeFragment = new HomeFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, homeFragment, "HOME_FRAGMENT")
                    .commit();
        }

        // 首页按钮点击事件
        btnNavHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFragment(FRAGMENT_HOME);
            }
        });

        // 我的按钮点击事件
        btnNavProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFragment(FRAGMENT_PROFILE);
            }
        });
        
        // 更新导航按钮状态
        updateNavigationButtons();
    }

    /**
     * 显示指定Fragment
     */
    private void showFragment(int fragmentType) {
        currentFragment = fragmentType;
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        // 隐藏所有Fragment
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (profileFragment != null) {
            transaction.hide(profileFragment);
        }

        // 显示指定Fragment
        if (fragmentType == FRAGMENT_HOME) {
            if (homeFragment == null) {
                homeFragment = new HomeFragment();
                transaction.add(R.id.fragment_container, homeFragment, "HOME_FRAGMENT");
            } else {
                transaction.show(homeFragment);
            }
        } else if (fragmentType == FRAGMENT_PROFILE) {
            if (profileFragment == null) {
                profileFragment = new ProfileFragment();
                transaction.add(R.id.fragment_container, profileFragment, "PROFILE_FRAGMENT");
            } else {
                transaction.show(profileFragment);
            }
        }

        transaction.commit();
        updateNavigationButtons();
    }

    /**
     * 更新导航按钮状态
     */
    private void updateNavigationButtons() {
        if (currentFragment == FRAGMENT_HOME) {
            btnNavHome.setEnabled(false);
            btnNavProfile.setEnabled(true);
        } else {
            btnNavHome.setEnabled(true);
            btnNavProfile.setEnabled(false);
        }
    }

    /**
     * 刷新首页Fragment(兴趣更新时调用)
     * 注:由于HomeFragment现在使用ViewModel管理数据,数据会自动同步,无需手动刷新
     */
    public void refreshHomeFragment() {
        // HomeFragment使用ViewModel+LiveData自动管理数据,无需手动刷新
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // 保存当前Fragment状态
        outState.putInt(KEY_CURRENT_FRAGMENT, currentFragment);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.d(TAG, "onNewIntent");
        // 处理singleTask模式下的新Intent
        setIntent(intent);
    }
}
